package com.dingding.service.common;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dingding.common.core.beans.Phonevalidate;
import com.dingding.common.core.sms.model.MessageModel;
import com.dingding.common.core.util.JSONUtil;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facade.queue.dto.msg.MessageRequestsDTO;
import com.dingding.facade.queue.facade.IQueueFacade;
import com.dingding.facde.common.IPhonevalidateService;
import com.dingding.service.common.core.dao.IPhonevalidateDao;

@Component("phonevalidateService")
public class PhonevalidateServiceImpl implements IPhonevalidateService {

	private static final Logger log = Logger.getLogger(PhonevalidateServiceImpl.class);

	@Resource(name = "phonevalidateDao")
	private IPhonevalidateDao phonevalidateDao;

	@Resource(name = "queueFacade")
	private IQueueFacade queueFacade;

	/**
	 * @Title: getVerifyCode 
	 * @Description: 获取验证码
	 * @param tel
	 * @author YHQ
	 * @return boolean    返回类型 
	 */
	@Override
	public boolean getVerifyCode(String tel) {

		if (ToolUtil.isBlank(tel)) {
			log.error("手机号码为空！PhonevalidateServiceImpl:getVerifyCode()");
			return false;
		}

		// 获取手机验证码
		Phonevalidate phonevalidate = this.phonevalidateDao.generatePhoneValidation(tel);

		// 创建短信内容
		String msg = MessageModel.registered(phonevalidate.getValidateNum());
		//添加到消息队列
		MessageRequestsDTO messageRequestsDTO = new MessageRequestsDTO();
		messageRequestsDTO.setTelPhone(tel);
		messageRequestsDTO.setMessageContent(msg);
		return queueFacade.addMessageToQueue(JSONUtil.toJSONString(messageRequestsDTO));

	}

	/**
	 * @Title: verifyCode 
	 * @Description: 验证验证码是否输入正确
	 * @param tel 手机号
	 * @param code 验证码
	 * @author YHQ
	 * @return boolean    返回类型 
	 */
	@Override
	public boolean verifyCode(String tel, String code) {

		if (ToolUtil.validateParas(tel, code)) {
			log.error("验证手机号码时，手机号或验证码为空！PhonevalidateServiceImpl:verifyCode()");
			return false;
		}

		return this.phonevalidateDao.verificationCode(tel, code);
	}

}
